package com.xzx.shop_front_api.controller;

import com.xzx.shop_front_api.entity.Coupon;
import com.xzx.shop_front_api.entity.MemberCoupon;
import com.xzx.shop_front_api.entity.vo.MyCouponVo;
import com.xzx.shop_front_api.service.CouponService;
import com.xzx.shop_front_api.service.MemberCouponService;
import com.xzx.shop_front_api.utils.InstantConverter;
import com.xzx.shop_front_api.utils.JsonResult;
import com.xzx.shop_front_api.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * 优惠券控制器类
 */
@RestController
@RequestMapping("api/coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;
    @Autowired
    private MemberCoupon MemberCoupon;
    @Autowired
    private MemberCoupon memberCoupon;
    @Autowired
    private MemberCouponService memberCouponService;

    @Autowired
    private MyCouponVo myCouponVo;

    @PostMapping("add")
    public JsonResult addCoupon(@RequestBody Coupon coupon) throws ParseException {
        //参数验证
        if (coupon.getTitle().isEmpty()) {
            return JsonResult.failed("优惠券标题不能为空");
        }

        if (coupon.getValue() == 0) {
            return JsonResult.failed("优惠券面值不能为空");
        }
        if (coupon.getStrStartTime().isEmpty()|| coupon.getStrEndTime().isEmpty()) {
            return JsonResult.failed("有效时间必选");
        }
        if (coupon.getMinMoney()==0) {
            return JsonResult.failed("使用条件不能为空");
        }
        coupon.setCreatedTime(Tool.getCurrentTime());
        coupon.setStatus(0);
        coupon.setStartTime(InstantConverter.toInteger(coupon.getStrStartTime()));
        coupon.setEndTime(InstantConverter.toInteger(coupon.getStrEndTime())+60*60*24);
        System.out.println(coupon);
        int result = couponService.insertCoupon(coupon);
        if (result == 0) {
            return JsonResult.failed("创建失败");
        }
        return JsonResult.success("创建成功", null);
    }

    //优惠券列表接口
    @GetMapping("list")
    public JsonResult listCoupon() {
        List<Coupon> couponList = couponService.couponList(Tool.getCurrentTime(), 1);
        for (Coupon coupon : couponList) {
            coupon.setStrStartTime(Tool.timeToDate(coupon.getStartTime()));
            coupon.setStrEndTime(Tool.timeToDate(coupon.getEndTime()));

        }
        return JsonResult.success("ok",couponList);
    }
    //领取优惠券
    @PostMapping("get")
    public JsonResult getCoupon(@RequestParam("member_id") int memberId,
                                @RequestParam("coupon_id") int couponId
                                ) {

        //重复领取限制
        MemberCoupon couponInfo = memberCouponService.getCouponInfoByMemberIdCouponId(memberId,couponId);
        if (couponInfo != null) {
            return JsonResult.failed("领取失败,不能重复领取.");
        }
        memberCoupon.setMemberId(memberId);
        memberCoupon.setCouponId(couponId);
        memberCoupon.setGetTime(Tool.getCurrentTime());

        int result = memberCouponService.getCoupon(memberCoupon);
        if (result == 0) {
            return JsonResult.failed("领取失败");
        }
        return JsonResult.success("领取成功", null);
    }

    //我的优惠券列表
    @GetMapping("my_coupons")
    public JsonResult myCoupons(@RequestParam("member_id") int memberId
    ) {

        List<MemberCoupon> myCouponlList = memberCouponService.getCouponInfoByMemberId(memberId);

        List<MyCouponVo> myCouponVoList = new ArrayList<>();

        for (MemberCoupon memberCoupon : myCouponlList) {
            Coupon couponInfo = couponService.getCouponById(memberCoupon.getCouponId());
            myCouponVo.setId(memberCoupon.getId());
            myCouponVo.setCoupon_id(couponInfo.getId());
            myCouponVo.setStatus(memberCoupon.getStatus());
            myCouponVo.setTitle(couponInfo.getTitle());
            myCouponVo.setValue(couponInfo.getValue());
            myCouponVo.setStart_time(Tool.timeToDate(couponInfo.getStartTime()));
            myCouponVo.setEnd_time(Tool.timeToDate(couponInfo.getEndTime()));
            myCouponVoList.add(myCouponVo);
        }
        return JsonResult.success("查询成功", myCouponVoList);
    }

    //memberid,有效，未使用的
    //可使用的优惠券列表
    @GetMapping("canuse_coupon_list")
    public JsonResult canUseCouponList(@RequestParam("member_id") int memberId,
                                       @RequestParam("amount") Double amount
                                       ) {
        //当前时间
        int currentTime = Tool.getCurrentTime();
        List<MemberCoupon> couponList = memberCouponService.canUseCouponList(memberId,0);
        for (MemberCoupon memberCoupon : couponList) {
            if (memberCoupon.getStatus()==1) {
                continue;
            }
            //根据优惠券id获取信息
            Coupon couponInfo = couponService.getCouponById(memberCoupon.getCouponId());
            System.out.println(couponInfo);

            if (couponInfo != null) {
                System.out.println(couponInfo);
                int startTime = couponInfo.getStartTime();//开始时间
                int endTime = couponInfo.getEndTime();
                //优惠券没失效，已经启用，购买金额符合条件
                System.out.println(amount<couponInfo.getMinMoney());
                if (currentTime < startTime || currentTime > endTime) {
                    continue;
                }
                if ( amount<couponInfo.getMinMoney()) {
                    continue;
                }
                memberCoupon.setCouponName(couponInfo.getTitle());
                memberCoupon.setValue(couponInfo.getValue());
            }

        }
        return JsonResult.success("ok",couponList);
    }
}
